package
{
	import flash.display.MovieClip;
	import flash.geom.Point;

	public class InterfButton
	{
		public static var BUTTON_STATE_NOTHIMG:int = 0;
		public static var BUTTON_STATE_START:int = 1;
		public static var BUTTON_STATE_ACTIVE:int = 2;
		public static var BUTTON_STATE_END:int = 3;
		
		public var posL:Point = new Point();
		public var posR:Point = new Point();
		
		public var clip:MovieClip;
		public var id:int;
		public var state:int;
		public var m_touchRegistered:Boolean;
		public var m_visible:Boolean;
		private var m_enable:Boolean;
		public var m_hasfocus:Boolean;
		private var text:String = null;
		
		public function InterfButton()
		{
			m_touchRegistered = false;
			m_visible = true;
			m_hasfocus = false;
			Enable(true);
		}
		
		
		public function Enable(b:Boolean):void
		{
			m_enable = b;			
		}
		
		public function IsEnable():Boolean
		{
			return m_enable;			
		}
		public function SetFocus(b:Boolean):void
		{
			m_hasfocus = b;
			if(m_hasfocus == true)
			{
				clip.nextFrame();
			}
			if(m_hasfocus == false)
			{
				clip.prevFrame();
			}
			
		}
		
		public function HasFocus():Boolean
		{
			return m_hasfocus;
		}
		
		public function SetText(str:String):void
		{
			text = str;
		}
		
		public function GetText():String
		{
			return text ;
		}
		
		public function HasText():Boolean
		{
			return 	text!= null;
		}
		
		public function GetCenterX():int
		{
			return posL.x + (posR.x - posL.x)/2;
		}
		
		public function GetCenterY():int
		{
			return posL.y + (posR.y - posL.y)/2;
		}
		
		public function HitTest(posX:int, posY:int):Boolean
		{
			return  (posX >= posL.x && posY >= posL.y) &&
				(posX <= posR.x && posY <= posR.y);
		}
		
		public function OnTouchDown(posX:int, posY:int):void
		{
			if( state == BUTTON_STATE_NOTHIMG || state == BUTTON_STATE_END )
			{
				state = BUTTON_STATE_START;
				//SetFlashing(false);
			}
			else
			{
				state = BUTTON_STATE_ACTIVE;
				//SetFlashing(false);
			}
		}
		
		public function OnTouchUp(posX:int, posY:int, inside:Boolean):void
		{			
			if(inside)
			{
				switch(state )
				{
					case BUTTON_STATE_START:
					case BUTTON_STATE_ACTIVE:
					{
						//m_endStateFrames = 0;
						state = BUTTON_STATE_END;
					}
					break;
				}
			}
			else
			{
				state = BUTTON_STATE_NOTHIMG;
			}
		}
		
		public function  OnTouchDragging ( pointerX:int, pointerY:int, pointerDX:int, pointerDY:int):void
		{			
			if( state == BUTTON_STATE_NOTHIMG || state == BUTTON_STATE_END )
			{
				state = BUTTON_STATE_START;
			}
		}
	}
}